package chapter2.version2;

import chapter2.Apple;

import java.util.ArrayList;
import java.util.List;

/**
 * 行为参数化
 */
public class AppleVersionTwo {

    /**
     * 根据抽象条件筛选
     * @param inventory
     * @param p
     * @return
     */
    public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p){
        List<Apple> result = new ArrayList<>();
        for(Apple apple: inventory){
            if (p.test(apple)){
                result.add(apple);
            }
        }
        return result;
    }

    public static <T> List<T> filter(List<T> list, Predicate<T> p){
        List<T> result = new ArrayList<>();
        for(T e: list){
            if (p.test(e)){
                result.add(e);
            }
        }
        return result;
    }
}
